package org.tigris.atlas.beehive;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.beehive.netui.pageflow.Forward;
import org.tigris.atlas.service.EntityCollectionServiceResponse;
import org.tigris.atlas.service.EntityServiceResponse;
import org.tigris.atlas.web.struts.InputForm;

/**
 * Base class for entity maintenance for multiple entities
 * 
 * @author sandrews
 *
 */
public abstract class EntityListMaintenancePageFlowController extends EntityMaintenancePageFlowController {

	private static final int DEFAULT_PAGE_SIZE = 10;
	private static final String ROW_LIST_SESSION_KEY = "rowListPage";
	
	private Integer pageSize;
	private List rowListPage = new ArrayList();

	public Forward begin() {
		loadEntities();
		return determineForward();
	}
	
	public Forward printerFriendlyListAction() { 
		getRequest().setAttribute(getTableSessionKey(), getRowListPage());
		return determineForward();	
	}
	
	public Forward returnToListAction() {        
        return determineForward();
	}
	
	private void loadEntities() {
		EntityCollectionServiceResponse response = lookupEntities();
		handleMessages(response);
		
		Collection entities = response.getEntities();
        getSession().removeAttribute( getTableSessionKey() );
        getSession().setAttribute( getTableSessionKey(), entities );
	}
	
	protected List getRowListPage() {
		Object rowList = getSession().getAttribute(ROW_LIST_SESSION_KEY);
		if (rowList != null) {
			rowListPage.clear();
			rowListPage.addAll((List) rowList);
			getSession().removeAttribute(ROW_LIST_SESSION_KEY);
		}
		return Collections.unmodifiableList(rowListPage);
	}
	
	protected abstract EntityCollectionServiceResponse lookupEntities();
	
	protected final void handleMessages(EntityCollectionServiceResponse response) {
		getMessagesHandler().handleMessages(response.getMessages(), getFormClass(), getRequest());
	}
	
	public Forward handleDisplayTagAction() {
		return determineForward();
	}
	
	public Forward saveAndAddAction(InputForm form) {
		populateEntity(form);
        EntityServiceResponse response = saveEntity();
		setCurrentEntity(response.getEntity());
        handleMessages( response, form );
        Forward forward = determineForward();
        
        if (forward.getName().equals(PageFlowControllerConstants.FORWARD_SUCCESS)) {
        	setCurrentEntity(createNewTOInstance());
        	setupNewEntity();
        	form.reset(getMapping(), getRequest());
        }
        
        return forward;
	}
	
	public Forward updatePageSize() {
		String sSize = getRequest().getParameter("pageSize");
		if (sSize != null) {
			setPageSize(Integer.parseInt(sSize));
		} else {
			setPageSize(DEFAULT_PAGE_SIZE);
		}
		return determineForward();
	}
	
	public boolean showAllRecords() {
		return getPageSize() < 0;
	}

	protected abstract String getTableSessionKey();

	public int getPageSize() {
		if (this.pageSize == null) {
			pageSize = new Integer(DEFAULT_PAGE_SIZE);
		}
		return pageSize.intValue();
	}

	public void setPageSize(int pageSize) {
		this.pageSize = new Integer(pageSize);
	}
	
}
